package com.juhuixing.common.assembly.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.util.Date;

/**
 * @ClassName MetaObjectHandlerConfig
 * @Description TODO
 * @Author 刘猛
 * @Date 2019/5/10 17:05
 **/
@Component
public class MetaObjectHandlerConfig implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {

        Object createTime = getFieldValByName("createTime", metaObject);
        Object updateTime = getFieldValByName("updateTime", metaObject);
        if (createTime == null) {
            setFieldValByName("createTime",new Timestamp(System.currentTimeMillis()), metaObject);
        }
        if (updateTime == null) {
            setFieldValByName("updateTime",new Timestamp(System.currentTimeMillis()), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Object updateTime = getFieldValByName("updateTime", metaObject);
        if (updateTime == null) {
            setFieldValByName("updateTime",new Timestamp(System.currentTimeMillis()), metaObject);
        }
    }
}
